// uploadConfig.js
const multer = require('multer');
const path = require('path');

// 设置存储配置
const storage = multer.diskStorage({
  destination: (req, file, cb) => {
    cb(null, 'public/loads');
  },
//   filename: (req, file, cb) => {
//     const uniqueSuffix = Date.now() + '-' + Math.round(Math.random() * 1E9);
//     cb(null, file.fieldname + '-' + uniqueSuffix + path.extname(file.originalname));
//   }
filename: (req, file, cb) => {
    //提取文件后缀
const ext = path.extname(file.originalname);
    //生成唯一的文件名
    const uniqueSuffix = Date.now() + '-' + Math.round(Math.random() * 1E9);
    //提取文件名
    const fileName = path.basename(file.originalname, ext);
    cb(null, fileName + '-' + uniqueSuffix + ext);
  }
});

const upload = multer({ storage: storage });

module.exports = upload;
